<template>
  <div>
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button style="float:right" type="primary" @click="czsearch()" size="small">查询搜索</el-button>
        <el-button
          style="float:right;margin-right: 15px"
          @click="handleResetSearch()"
          size="small"
        >重置</el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label>
            <el-select v-model="formInline.region" placeholder="搜索类型">
              <el-option label="餐桌编号" value="id"></el-option>
              <el-option label="餐桌类型" value="type"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="搜索内容">
            <el-input v-model="formInline.text" placeholder="请输入搜索内容"></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button size="mini" class="btn-add" @click="addcanzhuo()" style="float:right">添加餐桌</el-button>
    </el-card>
    <div class="table-container">
      <el-table
        ref="newProductTable"
        :data="currentData"
        style="width: 100%;"
        v-loading="listLoading"
        border
      >
        <el-table-column label="餐桌ID" width="150" align="center">
          <template slot-scope="scope">{{scope.row.id}}</template>
        </el-table-column>
        <el-table-column label="餐桌台号" width="150" align="center">
          <template slot-scope="scope">{{scope.row.number}}</template>
        </el-table-column>
        <el-table-column label="限坐人数" align="center">
          <template slot-scope="scope">{{scope.row.peopleNum}}</template>
        </el-table-column>
        <el-table-column label="状态" width="160" align="center">
          <template slot-scope="scope">{{scope.row.state}}</template>
        </el-table-column>
        <el-table-column label="类型" width="160" align="center">
          <template slot-scope="scope">{{scope.row.type}}</template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <!-- 
            background---设置背景颜色
            hide-on-single-page---只有一页时隐藏分页
            :total---总数据数量
            :page-size---每页显示数据的条数
            :pager-count---最大可见页码数
            @current-change---currentPage改变时会触发(会自动获得页码)
            :current-page---当前页
        -->
        <el-pagination
          background
          hide-on-single-page
          layout="prev, pager, next"
          :total="pageNum"
          :page-size="pageSize"
          :pager-count="pageCount"
          @current-change="changePage"
          :current-page="currentPage"
        ></el-pagination>
      </div>
    </div>
    <!-- Form -->
    <el-dialog title="请输入餐桌信息" :visible.sync="dialogFormVisible">
      <el-form :model="czform">
        <el-form-item label="餐桌台号" :label-width="formLabelWidth">
          <el-input v-model="czform.number" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="限坐人数" :label-width="formLabelWidth">
          <el-input v-model="czform.peopleNum" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="餐桌类型" :label-width="formLabelWidth">
          <el-select v-model="czform.type" placeholder="请选择餐桌区域">
            <el-option label="大厅" value="大厅"></el-option>
            <el-option label="包间" value="包间"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addcz()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 餐桌编辑 -->
    <el-dialog title="请编辑餐桌信息" :visible.sync="editFormVisible">
      <el-form :model="editform">
        <el-form-item label="餐桌ID" :label-width="formLabelWidth">
          <el-input v-model="editform.id" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="餐桌台号" :label-width="formLabelWidth">
          <el-input v-model="editform.number" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用餐人数" :label-width="formLabelWidth">
          <el-input v-model="editform.peopleNum" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="餐桌类型" :label-width="formLabelWidth">
          <el-select v-model="editform.type" placeholder="请选择">
            <el-option label="大厅" value="大厅"></el-option>
            <el-option label="包间" value="包间"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="餐桌状态" :label-width="formLabelWidth">
          <el-select v-model="editform.state" placeholder="请选择">
            <el-option label="可坐" value="可坐"></el-option>
            <el-option label="占用" value="占用"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editczbt()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapActions } from "vuex";
export default {
  name: "czSet",
  data() {
    return {
      formInline: {
        //查询
        text: "",
        region: ""
      },
      listLoading: false,
      dialogFormVisible: false,
      czform: {
        //添加餐桌
        number: "",
        peopleNum: "",
        typeId: 0,
        type: "",
        state: "可坐"
      },
      formLabelWidth: "120px",
      //餐桌编辑
      editform: {
        id: "",
        number: "",
        peopleNum: "",
        state: "",
        type: ""
      },
      editFormVisible: false,
      //分页
      currentPage: 1, //当前页
      pageSize: 5, //显示多少条
      pageCount: 7 //页码最大显示多少数
    };
  },
  computed: {
    currentData() {
      let start = (this.currentPage - 1) * 5;
      let end = (this.currentPage - 1) * 5 + 5;
      return this.$store.state.bsStore.czData.slice(start, end);
    },
    pageNum() {
      return this.$store.state.bsStore.czData.length;
    }
  },
  methods: {
    ...mapActions([
      "getczData",
      "addczData",
      "editczData",
      "czsearchData",
      "delCz"
    ]),
    //添加餐桌M
    addcanzhuo() {
      // console.log("弹出餐桌模态框");
      this.dialogFormVisible = true; //弹出模态框
    },
    //添加餐桌
    addcz() {
      this.dialogFormVisible = false;
      this.addczData(this.czform).then(res => {
        if (res === 200) {
          this.getczData()
          this.$message({
            type: "success",
            message: "添加成功!"
          });
        }
      });
    },
    //重置搜索
    handleResetSearch() {
      this.formInline.text = "";
      this.formInline.region = "";
      this.getczData()
    },
    //餐桌删除BTN
    handleDelete(index, row) {
      this.$confirm("是否要删除该餐桌?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let obj = {};
          obj.id = row.id;
          this.delCz(obj).then(res => {
            if (res === 200) {
              this.getczData()
              this.$message({
                type: "success",
                message: "删除成功!"
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //餐桌编辑
    handleEdit(index, row) {
      this.editFormVisible = true;
      this.editform = JSON.parse(JSON.stringify(row));
    },
    //餐桌编辑BTN
    editczbt() {
      this.editFormVisible = false;
      this.editczData(this.editform).then(res => {
        if (res === 200) {
          this.getczData()
          this.$message({
            type: "success",
            message: "修改成功!"
          });
        } else {
          this.$message({
            type: "success",
            message: "修改失败!"
          });
        }
      });
    },
    //分页目标页
    changePage(val) {
      this.currentPage = val;
    },
    //餐桌查询
    czsearch() {
      // this.czsearchData(this.formInline).then(res => {
      //   if (res === 200) {
      //    
      //   }
      // });
      let { text,region} = this.formInline
      this.$store.state.bsStore.czData = (this.$store.state.bsStore.czData.filter(item => item[region] == text))
    }
  },
  //请求餐桌数据
  created() {
    this.getczData();
  }
};
</script>

<style scoped>
.operate-container {
  margin: 15px 0;
}
.el-input {
  width: 300px;
}
.block {
  margin-left: 50%;
}
</style>